#include "FS.h"
#include "SD.h"
#include "SPI.h"

// Define the CS pin for the SD card

void setup() {
    Serial.begin(115200);

    // Initialize the SD card
    if (!SD.begin(SD_CS_PIN, SPI, 8000000)) {
        Serial.println("SD card initialization failed!");
        return;
    }
    Serial.println("SD card initialized successfully!");

    // Get SD card capacity information
    uint64_t totalBytes = SD.totalBytes();
    uint64_t usedBytes = SD.usedBytes();
    uint64_t freeBytes = totalBytes - usedBytes;

    Serial.print("Total SD card capacity: ");
    Serial.print(totalBytes / (1024 * 1024));
    Serial.println(" MB");

    Serial.print("Used SD card capacity: ");
    Serial.print(usedBytes / (1024 * 1024));
    Serial.println(" MB");

    Serial.print("Available SD card capacity: ");
    Serial.print(freeBytes / (1024 * 1024));
    Serial.println(" MB");

    // Create a file and write content
    File file = SD.open("/test.txt", FILE_WRITE);
    if (!file) {
        Serial.println("Failed to create file!");
        return;
    }
    file.println("Hello, SD Card!");
    file.close();
    Serial.println("File written successfully!");

    // Read file content
    file = SD.open("/test.txt");
    if (file) {
        Serial.println("File content:");
        while (file.available()) {
            Serial.write(file.read());
        }
        file.close();
    } else {
        Serial.println("Failed to open file for reading!");
    }

    // Delete the file
    if (SD.remove("/test.txt")) {
        Serial.println("File deleted successfully!");
    } else {
        Serial.println("Failed to delete file!");
    }
}

void loop() {
    // Nothing to do here
}    